import os
import threading
import time

import pynotify

import language
import tvdb
import downloader

class Notificator(threading.Thread):

    image = ''
    dire = ''
    icons = []
    plugins = None
    downloads = []

    def __init__(self):
        threading.Thread.__init__(self)
        pynotify.init("PyTv")
        self.execute = self.main
        self.lang = language.setup()

    def run(self):
        self.execute()

    def main(self):
        db = tvdb.TvDB()
        config = db.read_configuration()
        self.sleep = config[2] * 60 * 60
        self.onDay = config[1]
        self.down = config[3]
        self.direct = config[4]
        self.icon = config[5]
        self.host = config[6]
        self.format = config[7]
        
        #Delete old episodes info
        if config[0] == 1:
            db.delete_old_episodes()
            
        db.check_for_new_info()
        while True:
            calendars = db.check_new_episode(self.onDay)
            self._check_new_episodes(calendars, db)
            time.sleep(self.sleep)

    def refresh(self):
        db = tvdb.TvDB()
        config = db.read_configuration()
        self.onDay = config[1]
        self.down = config[3]
        self.direct = config[4]
        self.icon = config[5]
        self.host = config[6]
        self.format = config[7]
        db.check_for_new_info()
        calendars = db.check_new_episode(self.onDay)
        self._check_new_episodes(calendars, db)

    def _check_new_episodes(self, calendars, db):
        if self.icon == 1:
            Notificator.icons = os.listdir(Notificator.dire)
        download = downloader.Downloader()
        for cal in calendars:
            serie = cal.serie.upper() + ': ' + self.lang['newEpi']
            episode = self.lang['season'] + ': ' + str(cal.season) + '\n'\
             + self.lang['episode'] + ': ' + str(cal.nroEp) + ' - ' + cal.episode
            serieIcon = None 
            if self.icon == 1:
                if cal.serie.replace(' ', '+') not in Notificator.icons:
                    download.download_image(Notificator.dire, cal.serie)
                serieIcon = os.path.abspath(Notificator.dire + cal.serie.replace(' ', '+'))
            self.show_message(serie, episode, serieIcon)
        if self.down == 1:
            Notificator.downloads = download.obtain_downloads(calendars, self.direct, self.host, self.format)
            if len(self.downloads) > 0:
                self.show_message(self.lang['newDownload'][0], self.lang['newDownload'][1])
        Notificator.plugins.thread_plugins(db, self, Notificator.dire + 'plugins/')

    def download_info(self):
        return Notificator.downloads

    def set_new_configuration(self, configSleep, configOnDay, downs, ic, dir, h, form):
        self.sleep = configSleep * 60 * 60
        self.onDay = configOnDay
        self.down = downs
        self.direct = dir
        self.icon = ic
        self.host = h
        self.format = form

    def show_message(self, serie, episode, icon=None):
        if icon is not None:
            notif = pynotify.Notification(serie, episode, icon)
        else:
            notif = pynotify.Notification(serie, episode, Notificator.image)
        notif.set_timeout(400)
        notif.show()

    def error_message(self, serie):
        serie = serie.upper() + ': ' + self.lang['notFound'] + ': diego.sarmentero@gmail.com'
        self.show_message('PyTV Error!', serie)
